﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.Reflection;
using CommonTypes.Server;
using System.Collections;
using CommonTypes.Client;
using Client.Model;


namespace Client.Menus
{
    public partial class ClientMainMenu : Form
    {
        private ClientData _clientData;

        public ClientMainMenu(ClientData clientData)
        {
            InitializeComponent();

            _clientData = clientData;

            for (int index = 1; index <= 8760; index++)
            {
                timeSlots.Items.Add(index);
            }

            labelClientName.Text = _clientData.ClientId;
        }

        private void makeReservation_Click(object sender, EventArgs e)
        {
            if (invitedUsers.Text.Length == 0)
            {
                MessageBox.Show("No client were selected to be invited to the reservation.");
            }

            ArrayList slotList = new ArrayList();
            CheckedListBox.CheckedItemCollection reservationSlots = timeSlots.CheckedItems;
            for (int index = 0; index < reservationSlots.Count; index++)
            {
                ReservationSlot slot = new ReservationSlot((int)reservationSlots[index]);
                slotList.Add(slot);
            }

            String[] invitedUsersString = invitedUsers.Text.Split(',');
            ArrayList invitedUsersList = new ArrayList();

            for (int index = 0; index < invitedUsersString.Length; index++)
            {
                invitedUsersList.Add(invitedUsersString[index]);
            }

            Reservation reservation = new Reservation(_clientData.ClientId, 0, slotList, invitedUsersList, reservationDesc.Text);

            _clientData.AddNonInitiatedReservation(reservation);

            IClientToServer clientToServer = _clientData.GetRandomServer();
            clientToServer.GenerateTicket(_clientData.ClientId, reservation.ReservationTimestamp);
        }

        private void buttonViewCalendar_Click(object sender, EventArgs e)
        {
            new CalendarViewer(_clientData.ClientId, _clientData.ClientSlots).ShowDialog();
        }
    }
}
